

jQuery.fn.extend({
ImageRotate:function(parameters)
{	
	if (this.GUIPhoto&&this.GUIPhoto.PhotoEffect) return;
	return (new GUIPhoto.PhotoEffect(this,parameters))._temp;
},
rotate:function(parameters)
{
	if (typeof parameters=="undefined") return;
	if (typeof parameters=="number") parameters={angle:parameters};
	if (typeof this.get(0).GUIPhoto == "undefined") 
		return $(this.ImageRotate(parameters));
	else 
	{
		this.get(0).GUIPhoto.PhotoEffect._rotate(parameters);
	}
}

});

GUIPhoto={};

GUIPhoto.PhotoEffect=function(img,parameters)
{
			img = img.get(0);
			this._IEfix=img;
			this._parameters=parameters;
			this._parameters.className=img.className;
			this._parameters.id=img.getAttribute('id');
			
			if (!parameters) this._parameters={};
			this._angle=0;
			if (!parameters.angle) this._parameters.angle=0;
			this._temp=document.createElement('span');
			this._temp.GUIPhoto = 
				{
					PhotoEffect: this
				};			
			var image=img.src;
			img.parentNode.insertBefore(this._temp,img);
			this._img= new Image();
			this._img.src=image;
			this._img._ref=this;
			jQuery(this._img).bind("load", function()
			{
				this._ref._Loader.call(this._ref);
			});
			if (jQuery.browser.msie) if (this._img.complete) this._Loader();
}

GUIPhoto.PhotoEffect.prototype._Loader=function ()
{
	this._IEfix.parentNode.removeChild(this._IEfix);
	this._temp.setAttribute('id',this._parameters.id);
	this._temp.className=this._parameters.className;
	var width=this._img.width;
	var height=this._img.height;
	
	this._img._widthMax=this._img._heightMax=Math.sqrt((height)*(height) + (width) * (width));

	this._canvas=document.createElement('canvas');
	this._canvas._ref=this;
	this._canvas.height=width;
	this._canvas.width=height;

	this._canvas.setAttribute('width',width);

	this._temp.appendChild(this._canvas);

	if (jQuery.browser.msie) 
		{	
			// ExCanvas to make it work in IE
			this._canvas.id="GUIPhoto.PhotoTemp";
			G_vmlCanvasManager.initElement(this._canvas);
			this._canvas=document.getElementById('GUIPhoto.PhotoTemp');
			this._canvas.id="";							
			this._canvas._ref=this;
		}
	var self = this;
	this._parameters.animateAngle=0;
	if (this._parameters.bind) 
	{
		for (var a in this._parameters.bind) if (this._parameters.bind.hasOwnProperty(a)) 
		for (var b in this._parameters.bind[a]) if (this._parameters.bind[a].hasOwnProperty(b)) 
		jQuery(this._canvas).bind(b,this._parameters.bind[a][b]);
	}
	this._cnv=this._canvas.getContext('2d');
	this._rotate(this._parameters);
}

GUIPhoto.PhotoEffect.prototype._rotate = function(parameters)

{
	var angle = parameters.angle || 0;
	var offsetShadow  = parameters.offsetShadow || 5;
	var opacityShadow = parameters.opacityShadow || 0.5;
	var tailleBordure = parameters.tailleBordure || 5;
    var radius = parameters.radius || 6;
	
  if (!this._img.width) return;
	if (typeof angle!="number") return;
	angle=(angle%360)* Math.PI / 180;
	
  var width       = this._img.width;
	var height      = this._img.height;
  var widthCadre  = width+(tailleBordure*2);
	var heightCadre = height+(tailleBordure*2);
	var widthAdd    = this._img._widthMax - width;
	var heightAdd   = this._img._heightMax - height;
	
  // clear canvas	
	this._canvas.width = width+widthAdd+offsetShadow+(tailleBordure*2)+radius;
	this._canvas.height = height+heightAdd+offsetShadow+(tailleBordure*2)+radius;

	//this._cnv.scale(0.8,0.8); // SCALE - if needed ;)
	
	// REMEMBER: all drawings are read from backwards.. so first function is translate, then rotate, then translate, translate..
	 
	//this._cnv.save();
	this._cnv.translate(widthAdd/2,heightAdd/2); // at least center image on screen
	this._cnv.translate(width/2,height/2);		  // we move image back to its orginal 
	this._cnv.rotate(angle);					  // rotate image
	this._cnv.translate(-width/2,-height/2);	  // move image to its center, so we can rotate around its center
	
	this._shadow(widthCadre,heightCadre,radius,offsetShadow,'rgba(0, 0, 0, ' + opacityShadow + ')');	//Shadow on
	//this._shadowMano(widthCadre,heightCadre,radius,offsetShadow,opacityShadow);
	this._cnv.fillStyle = '#FFF';
	this._cnv.fillRect(0,0,width+(tailleBordure*2),height+(tailleBordure*2));
	this._shadow(0,0,0,0,"transparent");	 //Shadow off
	this._cnv.drawImage(this._img, tailleBordure, tailleBordure);		  // First - we draw image
	
	if (parameters.callback)
		parameters.callback();
}


GUIPhoto.PhotoEffect.prototype._shadow = function(width,height,radius,distance,color)
{
	//Create a drop shadow
	this._cnv.shadowOffsetX = distance;
	this._cnv.shadowOffsetY = distance;
	this._cnv.shadowBlur = radius;
	this._cnv.shadowColor = color;
}

GUIPhoto.PhotoEffect.prototype._shadowMano = function(width,height,radius,distance,opacity)
{
  var unite = 1;
  var start = 2;
  var sortie = "";
  for (var i=start; i<=radius; i=i+2)
  {
    for (var j=start; j<=radius; j=j+2)  
    {
      if (i!=j)
      {
        sortie += i+' '+j+"\n";
        this._shadowLayer(distance-(i*unite)+radius,distance-(j*unite)+radius,width+(2*i*unite)-radius,height+(2*j*unite)-radius,(opacity/(radius)));
      }
    }
  }
}

GUIPhoto.PhotoEffect.prototype._shadowLayer = function(x,y,width,height,opacityLayer)
{
  this._cnv.fillStyle = 'rgba(0,0,0,'+opacityLayer+')';
	this._cnv.fillRect(x,y,width,height);
  
  this._cnv.clearRect(x,y,1,1);
  this._cnv.clearRect(x+width-1,y,1,1);
  this._cnv.clearRect(x,y+height-1,1,1);
  this._cnv.clearRect(x+width-1,y+height-1,1,1);  
}
